package liangtWorkSpace.simple;

import java.util.List;

/**
 * @ClassName MergeTwoLists
 * @Description : 将两个有序列归并为一个有序列
 * @Author : LiangT
 * @Date : 2020/6/15 17:57
 */
public class MergeTwoLists {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode listNode = new ListNode(0);
        ListNode cur = listNode;
        while (l1 != null && l2 != null){
            if (l1.val <= l2.val){
                cur.next=l1;
                cur = cur.next;
                l1 = l1.next;
            }else {
                cur.next=l2;
                cur = cur.next;
                l2 = l2.next;
            }
        }
        //任意一个为空再将另一个剩余的链表补上
        if (l1 == null) {
            cur.next = l2;
        }
        if (l2 == null){
            cur.next = l1;
        }
        return listNode.next;
    }

}
